import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
import LoginVue from '@/views/LoginVue.vue'
import RegisterVue from '@/views/RegisterVue.vue'
import ForgotPasswordVue from '@/views/ForgotPasswordVue.vue'
import HomeVue from '@/views/HomeVue.vue'
import AboutVue from '@/views/AboutVue.vue'
import ContactVue from '@/views/ContactVue.vue'
import PrivacyVue from '@/views/PrivacyVue.vue'
import TermsVue from '@/views/TermsVue.vue'
import HistoryVue from '@/views/HistoryVue.vue'
import MainLayout from '@/layout/MainLayout.vue'
import AdminNavBar from '@/views/Admin/AdminNavBar.vue'
import ProfileVue from '@/views/ProfileVue.vue'
import AdminHome from '@/views/Admin/AdminHome.vue'
import UserList from '@/views/Admin/User/UserList.vue'
import RoleList from '@/views/Admin/User/RoleList.vue'
import ShowList from '@/views/Admin/Show/ShowList.vue'
import ProgramList from '@/views/Admin/Show/ProgramList.vue'
import ProgramCategory from '@/views/Admin/Show/ProgramCategory.vue'
import ShowsVue from '@/views/ShowsVue.vue'
import ShowDetailVue from '@/views/ShowDetailVue.vue'
import GuestbookVue from '@/views/Admin/GuestbookVue.vue'
import ArtistsVue from '@/views/ArtistsVue.vue'
import ArtistDetailVue from '@/views/ArtistDetailVue.vue'
import SchoolsVue from '@/views/SchoolsVue.vue'
import KnowledgeVue from '@/views/KnowledgeVue.vue'
import CourseDetailVue from '@/views/CourseDetailVue.vue'
import ForumVue from '@/views/ForumVue.vue'
import PostDetailVue from '@/views/PostDetailVue.vue'
import AnnouncementsVue from '@/views/AnnouncementsVue.vue'
import BookMarkVue from '@/views/BookMarkVue.vue'

import ArtistList from '@/views/Admin/Artist/ArtistList.vue'
import SchoolList from '@/views/Admin/Artist/SchoolList.vue'
import CategoryList from '@/views/Admin/Artist/CategoryList.vue'
import AnnouncementAdminVue from '@/views/Admin/AnnouncementVue.vue'
import CategoriesVue from '@/views/CategoryVue.vue' 
import ClassicsVue from '@/views/ClassicsVue.vue'

// 剧目管理导入
import DramaList from '@/views/Admin/Drama/DramaList.vue'

// 知识管理导入
import ArticleManagement from '@/views/Admin/Content/ArticleManagement.vue'
import CategoryManagement from '@/views/Admin/Content/CategoryManagement.vue'

// 论坛管理导入
import TopicsList from '@/views/Admin/Forum/TopicsList.vue'
import PostsList from '@/views/Admin/Forum/PostsList.vue'
import CommentsList from '@/views/Admin/Forum/CommentsList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: MainLayout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: HomeVue
      },
      {
        path: '/about',
        name: 'About',
        component: AboutVue
      },
      {
        path: '/contact',
        name: 'Contact',
        component: ContactVue
      },
      {
        path: '/privacy',
        name: 'Privacy',
        component: PrivacyVue
      },
      {
        path: '/terms',
        name: 'Terms',
        component: TermsVue
      },
      {
        path: '/history',
        name: 'History',
        component: HistoryVue
      },
      {
        path: '/profile',
        name: 'Profile',
        component: ProfileVue
      },
      {
        path: '/shows',
        name: 'Shows',
        component: ShowsVue
      },
      {
        path: '/show/:id',
        name: 'ShowDetail',
        component: ShowDetailVue
      },
      {
        path: '/artists',
        name: 'Artists',
        component: ArtistsVue
      },
      {
        path: '/artist/:id',
        name: 'ArtistDetail',
        component: ArtistDetailVue
      },
      {
        path: '/categories',
        name: 'Categories',
        component: CategoriesVue
      },
      {
        path: '/classics',
        name: 'Classics',
        component: ClassicsVue
      },
      {
        path: '/schools',
        name: 'Schools',
        component: SchoolsVue
      },
      {
        path: '/knowledge',
        name: 'Knowledge',
        component: KnowledgeVue
      },
      {
        path: '/knowledge/course/:id',
        name: 'CourseDetail',
        component: CourseDetailVue
      },
      {
        path: '/forum',
        name: 'Forum',
        component: ForumVue
      },
      {
        path: '/forum/post/:id',
        name: 'PostDetail',
        component: PostDetailVue
      },
      {
        path: '/announcements',
        name: 'Announcements',
        component: AnnouncementsVue
      },
      {
        path: '/bookmark',
        name: 'BookMark',
        component: BookMarkVue
      },
      {
        path: '/my-posts',
        name: 'MyPosts',
        component: () => import('@/views/MyPostsVue.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录' },
    component: LoginVue
  },
  {
    path: '/register',
    name: 'Register',
    component: RegisterVue
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: ForgotPasswordVue
  },
  {
    path: '/admin',
    name: 'Admin',
    component: AdminNavBar,
    redirect: '/AdminHome',
    children: [
      {
        path: '/AdminHome',
        name: 'AdminHome',
        component: AdminHome
      },
      {
        path: '/user/userlist',
        name: 'UserList',
        component: UserList
      },
      {
        path: '/user/rolelist',
        name: 'RoleList',
        component: RoleList
      },
      {
        path: '/performances/showlist',
        name: 'ShowList',
        component: ShowList
      },
      {
        path: '/performances/programlist',
        name: 'ProgramList',
        component: ProgramList
      },
      {
        path: '/performances/programcategory',
        name: 'ProgramCategory',
        component: ProgramCategory
      },
      {
        path: '/guestbook',
        name: 'GuestbookVue',
        component: GuestbookVue
      },
      {
        path: '/announcementlist',
        name: 'AnnouncementsAdmin',
        component: AnnouncementAdminVue
      },
      {
        path: '/artists/artist/list',
        name: 'ArtistList',
        component: ArtistList,
      },
      {
        path: '/artists/school/list', 
        name: 'SchoolList',
        component: SchoolList,
      },
      {
        path: '/artists/category/list',
        name: 'CategoryList',
        component: CategoryList,
      },
      // 剧目管理路由
      {
        path: '/drama/list',
        name: 'DramaList',
        component: DramaList,
      },
      // 知识管理路由
      {
        path: '/content/articles',
        name: 'ArticleManagement',
        component: ArticleManagement,
      },
      {
        path: '/content/categories',
        name: 'CategoryManagement',
        component: CategoryManagement,
      },
      // 论坛管理路由
      {
        path: '/forum/topics',
        name: 'TopicsList',
        component: TopicsList,
      },
      {
        path: '/forum/posts',
        name: 'PostsList',
        component: PostsList,
      },
      {
        path: '/forum/comments',
        name: 'CommentsList',
        component: CommentsList,
      },
      
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 白名单：不需要登录就可以访问的路径
const whiteList = ['/login', '/register', '/forgot-password', '/', '/about', '/home', '/privacy', '/terms', '/history', '/knowledge', '/shows', '/artists', '/schools', '/classics', '/forum', '/announcements', '/bookmark']

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('token')
  
  // 处理管理系统访问权限
  if (to.path.startsWith('/admin') || to.path === '/AdminHome' || to.path.includes('/user/') || 
      to.path.includes('/performances/') || to.path.includes('/artists/') || 
      to.path.includes('/content/') || to.path.includes('/forum/topics') || 
      to.path.includes('/forum/posts') || to.path.includes('/forum/comments') || 
      to.path.includes('/drama/') || to.path === '/guestbook' || 
      to.path === '/announcementlist') {
    
    // 检查用户是否登录
    if (!token) {
      next('/login')
      Message.error("请先登录!")
      return
    }
    
    // 检查用户角色
    const roleType = localStorage.getItem('roletype')
    const isAdmin = roleType === '1'
    const isArtist = roleType === '3'
    
    // 如果是管理员，允许访问所有管理功能
    if (isAdmin) {
      next()
      return
    }
    
    // 如果是艺术家，只允许访问特定的管理功能
    if (isArtist) {
      // 艺术家只能访问的路径
      const artistAllowedPaths = [
        '/AdminHome',
        '/admin',
        '/content/articles',
        '/content/categories',
        '/drama/list',
        '/forum/posts',
        '/forum/topics',
        '/forum/comments'
      ]
      
      // 检查是否是艺术家可以访问的路径
      const isArtistAllowed = artistAllowedPaths.some(path => to.path.includes(path))
      if (isArtistAllowed) {
        next()
        return
      } else {
        // 如果艺术家尝试访问不允许的路径，跳转到首页
        next('/AdminHome')
        Message.error("您没有权限访问该页面")
        return
      }
    }
    
    // 其他角色不允许访问管理系统
    next('/')
    Message.error("您没有权限访问管理系统")
    return
  }
  
  if (token) {
    // 已登录状态
    if (to.path === '/login') {
      // 如果已登录，访问登录页则重定向到首页
      next('/')
    } else {
      next()
    }
  } else {
    // 未登录状态
    if (whiteList.includes(to.path) || to.path.startsWith('/show/') || to.path.startsWith('/artist/') || 
        to.path.startsWith('/knowledge/course/') || to.path.startsWith('/forum/topic/')) {
      // 如果是白名单页面或详情页，直接放行
      next()
    } else {
      // 其他页面重定向到登录页
      next('/login')
      Message.error("请先登录!")
    }
  }
})

export default router




